//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.promotion;

import com.aidong.dao.wills.MemberContractDao;
import com.aidong.dao.wills.MemberInfoDao;
import com.aidong.dao.wills.PromotionProductReceiveDao;
import com.aidong.model.wills.MemberContract;
import com.aidong.model.wills.MemberInfo;
import com.aidong.model.wills.PromotionProductReceive;
import com.aidong.web.controller.BaseController;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"promotionReceive"})
public class PromotionReceiveController extends BaseController {
    @Resource
    PromotionProductReceiveDao promotionProductReceiveDao;
    @Resource
    MemberInfoDao memberInfoDao;
    @Resource
    MemberContractDao memberContractDao;

    public PromotionReceiveController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return new ModelAndView("promotion/receive/list");
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "cardCode",required = false) String cardCode) {
        ModelAndView mv = new ModelAndView("promotion/receive/_list");
        if(!this.chkNull(cardCode)) {
            MemberInfo member = this.memberInfoDao.getByCardCode(cardCode);
            if(member != null) {
                List list = this.promotionProductReceiveDao.getProductByMember(member.getMemberCode());
                mv.addObject("list", list);
            }

            mv.addObject("memberCode", member.getMemberCode());
        }

        return mv;
    }

    @RequestMapping({"_recv"})
    @ResponseBody
    public String _recv(@RequestParam("memberCode") String memberCode, @RequestParam("prods") String prods) {
        ArrayList list = new ArrayList();
        HashMap contractMap = new HashMap();
        String[] ss = prods.split("\\|");
        String[] e = ss;
        int len$ = ss.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            String s1 = e[i$];
            String[] s = s1.split(",");
            if(s.length != 2) {
                return this.getResult(false, "数据错误");
            }

            MemberContract contract = (MemberContract)contractMap.get(s[0]);
            if(contract == null) {
                contract = this.memberContractDao.getByContractCode(s[0]);
                if(contract == null || !contract.getMemberCode().equalsIgnoreCase(memberCode)) {
                    return this.getResult(false, "合同号错误");
                }

                contractMap.put(s[0], contract);
            }

            if(this.promotionProductReceiveDao.chkProductReceive(contract.getContractCode(), s[1])) {
                return this.getResult(false, "已经领取");
            }

            PromotionProductReceive receive = new PromotionProductReceive();
            receive.setContractCode(contract.getContractCode());
            receive.setPromotionId(contract.getPromotionId());
            receive.setMemberCode(contract.getMemberCode());
            receive.setProductCode(s[1]);
            receive.setReceiveDate(new Date());
            list.add(receive);
        }

        if(list.size() == 0) {
            return this.getResult(false, "请选择礼品");
        } else {
            try {
                this.promotionProductReceiveDao.addReceive(list);
                this.addUserLog("营销活动礼品领取", "会员:" + memberCode);
            } catch (Exception var13) {
                this.log.error("add promotion receive", var13);
                return this.getResult(false, "发生错误");
            }

            return this.getResult(true, "领取完成");
        }
    }
}
